package com.example.yml0528;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yml.R;
import com.example.yml0528.Service.LoginService;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText name;
    private EditText pw;
    private CheckBox jz;
    private TextView wj;
    private TextView zc;
    private Button login;
    private User user;
    private Guang guang;
    private IntentFilter intentFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();


    }

    public void init() {
        name = findViewById(R.id.name);
        pw = findViewById(R.id.pw);
        jz = findViewById(R.id.jz);
        wj = findViewById(R.id.wj);
        zc = findViewById(R.id.zc);
        login = findViewById(R.id.login);

        wj.setOnClickListener(this);
        zc.setOnClickListener(this);
        login.setOnClickListener(this);
        user = new User();
        user.setUsername("admin");
        user.setPw("123456");
        user.setSex("男");
        user.setHobby("打游戏");
        user.setPhone("12345678998");
        guang = new Guang();
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.yml0528.LoginActivity");
        registerReceiver(guang, intentFilter);
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        String uname = sp.getString("name", null);
        String pass = sp.getString("pass", null);
        name.setText(uname);
        try {
            FileInputStream fileInputStream = this.openFileInput("user.txt");
            InputStreamReader read = new InputStreamReader(fileInputStream);
            BufferedReader bin = new BufferedReader(read);
            String s = bin.readLine();
            pw.setText(s);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111) {
            user = (User) data.getSerializableExtra("myuser");
            name.setText(user.getUsername());
            pw.setText(user.getPw());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wj:
                break;
            case R.id.zc:
                Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivityForResult(intent, 111);
                break;
            case R.id.login:

                //bindService(intentt,new MyConn(),BIND_ABOVE_CLIENT);
                Intent intentt = new Intent(this, LoginService.class);
                intentt.putExtra("name", name.getText().toString());
                intentt.putExtra("pass", pw.getText().toString());
                intentt.putExtra("user", user);

                if (jz.isChecked()) {
                    startService(intentt);
                } else {
                    Toast.makeText(LoginActivity.this, "请勾选记住密码", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    @Override
    protected void onStop() {
        unregisterReceiver(guang);
        super.onStop();
    }
}

